package com.ymatou.iapi.sellercs.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.sellercs.parameter.AppOffLineBean;
import com.ymatou.iapi.sellercs.parameter.SendMessageBean;
import com.ymatou.iapi.sellercs.service.AppOffLineCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.database.model.YmtCssession;
import com.ymttest.database.model.YmtOnlinecs;
import com.ymttest.database.redis.ImRedisWapper;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.GetLog;
import com.ymttest.utils.TestDataManager;

import redis.clients.jedis.Tuple;
/**
*App下线
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_AppOffLine{
	private static AppOffLineBean appofflineBean;
	private static AppOffLineCall appofflineCall;
	YmtSellerCSIMWapper YmtSellerCSIMWapper;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("App下线");
	}

	@Before
	public void caseUp() {
		appofflineBean=new AppOffLineBean();
		appofflineCall=new AppOffLineCall();
		YmtSellerCSIMWapper=new YmtSellerCSIMWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(appofflineCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_AppOffLine_001() {
		Logger.start(true,"新数据App下线成功,LoginStatus=1删除记录");
		try {
			Logger.comment("注册新用户");
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			Logger.comment("初始化数据");
//			Integer csid=3383;
//			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(sellerId);
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			SellerCSIMCallService.Assign();
			Thread.sleep(1000);
//			GetLog getLog=new GetLog("172.16.101.133", "sellercs.iapi.ymatou.com");
			//post 表单不接受requestid
//			getLog.startListenVm("default.log",".");
			//TODO OffLineType必填但是无用 枚举但是可以随便传
			appofflineBean.setUserId(sellerId);
			appofflineCall.setData(appofflineBean);
			appofflineCall.callService();			
			
			Logger.verifyIsContains("true", appofflineCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", appofflineCall.getString("Status"), "验证Status");
			Thread.sleep(3000);
//			getLog.endListen();
//			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
//			Logger.comment("验证日志包含发送指令op=0登出");
//			List<String> filter2 = loglist.stream().filter(s->s.contains("op=0")).collect(Collectors.toList());
//			Logger.debug(filter2);
//			Logger.verifyEquals(true,filter2.size()>=1,"验证日志");
			
			List<YmtOnlinecs> info = YmtSellerCSIMWapper.selectYmtOnlinecs(null, sellerId);
			Logger.debug(FormatUtil.beanToGSONString(info));
			Logger.verifyEquals(0, info.size(), "验证记录删除");
			Logger.comment("验证正常关闭会话");
			//会话有可能到历史表中
			List<YmtCssession> cinfos = YmtSellerCSIMWapper.selectYmtCssession(sendmessageBean.getToUserId(), sendmessageBean.getFromUserId());
			if(cinfos!=null&&cinfos.size()>0){
				Logger.verifyEquals(1, cinfos.get(0).getStatus(), "验证离线");
				Logger.verifyEquals(0, cinfos.get(0).getCloseType(), "验证关闭");
			}
			Logger.comment("验证redis数据");
			ImRedisWapper ImRedisWapper=new ImRedisWapper();
			Set<Tuple> set = ImRedisWapper.getCSession(sendmessageBean.getToUserId());
			set.forEach(t->{Logger.debug("tuple:"+t.getScore()+":"+t.getElement());});
			List<String> list = set.stream().map(t->{return t.getElement();}).collect(Collectors.toList());
			Logger.verifyEquals(false,list.contains(sendmessageBean.getFromUserId().toString()),"验证CSession中包含有FromUserId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AppOffLine_002() {
		Logger.start(true,"不输入userid失败");
		try {
			appofflineCall.setData(appofflineBean);
			appofflineCall.callService();			
			Logger.verifyIsContains("400", appofflineCall.getString("Status"), "验证Status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AppOffLine_003() {
		Logger.start(true,"App下线成功,LoginStatus=1不变");
		try {
			Logger.comment("初始化数据");
			Integer csid=3383;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			SellerCSIMCallService.AppLogin(csid);
			YmtSellerCSIMWapper.updateYmtOnlinecsSetLoginStatus(csid, 1);
			GetLog getLog=new GetLog("172.16.101.133", "sellercs.iapi.ymatou.com");
			//post 表单不接受requestid
			getLog.startListenVm("default.log",".");
			//TODO OffLineType必填但是无用 枚举但是可以随便传
			appofflineBean.setUserId(csid);
			appofflineCall.setData(appofflineBean);
			appofflineCall.callService();			
			
			Logger.verifyIsContains("true", appofflineCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", appofflineCall.getString("Status"), "验证Status");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
			//TODO AppOffLine 不发送selleronline 离线消息
//			Logger.comment("验证日志包含发送指令op=0登出");
//			List<String> filter2 = loglist.stream().filter(s->s.contains("op=0")).collect(Collectors.toList());
//			Logger.debug(filter2);
//			Logger.verifyEquals(true,filter2.size()>=1,"验证日志");
			

			List<YmtOnlinecs> infos = YmtSellerCSIMWapper.selectYmtOnlinecs(null, csid);
			Logger.verifyEquals(1, infos.size(), "验证记录未删除");
			Logger.debug(FormatUtil.beanToGSONString(infos));
			YmtOnlinecs info = infos.get(0);
			Logger.verifyEquals(csid, info.getCSId(), "验证csid");
			Logger.verifyEquals(mcsid, info.getMasterCSId(), "验证mcsid");
			
			Logger.verifyEquals(1, info.getLoginStatus(), "验证loginStatus为在线1");
			Logger.verifyEquals(0, info.getStatus(), "验证status为在线0");
			//时间允许误差
			Logger.verifyEquals(true,new Date().getTime()-info.getLastActiveTime().getTime()<10000,"验证最后时间");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AppOffLine_004() {
		Logger.start(true,"App下线成功,LoginStatus=3变1");
		try {
			Logger.comment("初始化数据");
			Integer csid=3383;
			Integer mcsid=3383;
			YmtSellerCSIMWapper.deleteYmtOnlinecsByCsid(csid);
			SellerCSIMCallService.AppLogin(csid);
			YmtSellerCSIMWapper.updateYmtOnlinecsSetLoginStatus(csid, 3);
			GetLog getLog=new GetLog("172.16.101.133", "sellercs.iapi.ymatou.com");
			//post 表单不接受requestid
			getLog.startListenVm("default.log",".");
			//TODO OffLineType必填但是无用 枚举但是可以随便传
			appofflineBean.setUserId(csid);
			appofflineCall.setData(appofflineBean);
			appofflineCall.callService();			
			
			Logger.verifyIsContains("true", appofflineCall.getString("Result"), "验证IsSuccess");
			Logger.verifyIsContains("200", appofflineCall.getString("Status"), "验证Status");
			Thread.sleep(3000);
			getLog.endListen();
			ArrayList<String> loglist = getLog.getLastLog();
			//验证日志
//			Logger.comment("验证日志包含发送指令op=0登出");
//			List<String> filter2 = loglist.stream().filter(s->s.contains("op=0")).collect(Collectors.toList());
//			Logger.debug(filter2);
//			Logger.verifyEquals(true,filter2.size()>=1,"验证日志");
			

			List<YmtOnlinecs> infos = YmtSellerCSIMWapper.selectYmtOnlinecs(null, csid);
			Logger.verifyEquals(1, infos.size(), "验证记录未删除");
			Logger.debug(FormatUtil.beanToGSONString(infos));
			YmtOnlinecs info = infos.get(0);
			Logger.verifyEquals(csid, info.getCSId(), "验证csid");
			Logger.verifyEquals(mcsid, info.getMasterCSId(), "验证mcsid");
			
			Logger.verifyEquals(1, info.getLoginStatus(), "验证loginStatus为在线1");
			Logger.verifyEquals(0, info.getStatus(), "验证status为在线0");
			//时间允许误差
			Logger.verifyEquals(true,new Date().getTime()-info.getLastActiveTime().getTime()<10000,"验证最后时间");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_AppOffLine_005() {
		Logger.start(true,"输入错误userid失败");
		try {
			appofflineBean.setUserId(-1);
			appofflineCall.setData(appofflineBean);
			appofflineCall.callService();			
			Logger.verifyIsContains("400", appofflineCall.getString("Status"), "验证Status");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}